
#include <string>
#include <iostream>
#include <stdexcept>
#include <math.h>
#include <FrameL.h>
#include "pyframe.hpp"

std::string getChannels(char *filename, int gps)
{
    std::string channels;
    FrFile *ifile;

    ifile = FrFileINew(filename);
    if(ifile==NULL) {
        throw;
    }
    channels = FrFileIGetChannelList(ifile,gps);
    FrFileIEnd(ifile);
    return channels;
}


void getY(char *filename, char *channel, int gps, int len, vector *out)
{
    FrFile *ifile;
    FrVect *vdata;
    unsigned int i;

    ifile = FrFileINew(filename);
    if(ifile==NULL) {
        throw std::runtime_error("Can't open input file");;
    }
    vdata = FrFileIGetVectD(ifile,channel,gps,len);
    if(vdata==NULL) {
        throw missing_data_error();
    }
    if(out->v->size != vdata->nData) {
        out->resize(vdata->nData);
    }
    for(i=0; i<vdata->nData; i++) {
        gsl_vector_set(out->v,i,vdata->dataD[i]);
    }
    if(vdata->next!=NULL) {
        throw missing_frame_error();
    }
    FrVectFree(vdata);
    FrFileIEnd(ifile);
}

void getY_missing(char *filename, char *channel, int gps, int len, vector *out, vector *miss)
{
    FrFile *ifile;
    FrVect *vdata;
    unsigned int i;

    ifile = FrFileINew(filename);
    if(ifile==NULL) {
        throw std::runtime_error("Can't open input file");
    }
    vdata = FrFileIGetVectD(ifile,channel,gps,len);
    if(vdata==NULL) {
        throw std::runtime_error("Can't get data from file");
    }
    if(out->v->size != vdata->nData) {
        out->resize(vdata->nData);
    }
    for(i=0; i<vdata->nData; i++) {
        gsl_vector_set(out->v,i,vdata->dataD[i]);
    }
    if(vdata->next!=NULL) {
        if(miss->v->size != vdata->next->nData) miss->resize(vdata->next->nData);
        for(i=0; i<vdata->next->nData; i++) {
            gsl_vector_set(miss->v,i,vdata->next->dataD[i]);
        }
    }
    FrVectFree(vdata);
    FrFileIEnd(ifile);
}

void getY_float(char *filename, char *channel, int gps, int len, vectorf *out)
{
    FrFile *ifile;
    FrVect *vdata;
    unsigned int i;

    ifile = FrFileINew(filename);
    if(ifile==NULL) {
        throw std::runtime_error("Can't open input file");;
    }
    vdata = FrFileIGetVectD(ifile,channel,gps,len);
    if(vdata==NULL) {
        throw missing_data_error();
    }
    if(out->v->size != vdata->nData) {
        out->resize(vdata->nData);
    }
    for(i=0; i<vdata->nData; i++) {
        gsl_vector_float_set(out->v,i,(float)vdata->dataD[i]);
    }
    if(vdata->next!=NULL) {
        throw missing_frame_error();
    }
    FrVectFree(vdata);
    FrFileIEnd(ifile);
}

void getY_missing_float(char *filename, char *channel, int gps, int len, vectorf *out, vectorf *miss)
{
    FrFile *ifile;
    FrVect *vdata;
    unsigned int i;

    ifile = FrFileINew(filename);
    if(ifile==NULL) {
        throw std::runtime_error("Can't open input file");
    }
    vdata = FrFileIGetVectD(ifile,channel,gps,len);
    if(vdata==NULL) {
        throw std::runtime_error("Can't get data from file");
    }
    if(out->v->size != vdata->nData) {
        out->resize(vdata->nData);
    }
    for(i=0; i<vdata->nData; i++) {
        gsl_vector_float_set(out->v,i,(float)vdata->dataD[i]);
    }
    if(vdata->next!=NULL) {
        if(miss->v->size != vdata->next->nData) miss->resize(vdata->next->nData);
        for(i=0; i<vdata->next->nData; i++) {
            gsl_vector_float_set(miss->v,i,vdata->next->dataD[i]);
        }
    }
    FrVectFree(vdata);
    FrFileIEnd(ifile);
}